<template>
  <div class="TermManagement">
    <div class="pubBig Privilege">
      <div class="h0">
        <p class="lt"></p>
      </div>
      <div class="dUpMain">

      </div>
      <div class="dDownMain">
        <div
          class="twoBox"
          :style="this.$route.query.Token ? 'top:50px;left:10px':'top: 100px'"
        >
          <div style="margin:15px">
            <span>关键字：</span>
            <el-input
              style="display: inline-block;width:15%"
              size="small"
              v-model="value"
            ></el-input>
            <el-button
              style="margin-left:15px"
              size="small"
              icon="el-icon-search"
              @click="getTableData"
            ></el-button>
            <el-button
              style="margin-left:15px"
              size="small"
              icon="el-icon-plus"
              @click="open"
            ></el-button>
          </div>
          <div>
            <template>
              <el-table
                :data="tableData"
                style="width: 100%"
              >
                <el-table-column
                  label="序号"
                  type="index"
                  :index='(index)=>{return page.limit*(page.page-1)+index + 1  }'
                >
                </el-table-column>
                <el-table-column
                  prop="HospitalId"
                  label="医院id"
                >
                </el-table-column>

                <el-table-column
                  prop="TermCode"
                  label="术语编码"
                >
                </el-table-column>
                <el-table-column
                  prop="TermName"
                  label="术语含义"
                >
                </el-table-column>
                <el-table-column
                  prop="Version"
                  label="版本号"
                >
                </el-table-column>
                <el-table-column
                  prop="Remark"
                  label="备注"
                >
                </el-table-column>
                <el-table-column
                  prop="CreateTime"
                  label="创建时间"
                >
                  <template slot-scope="scope">
                    {{scope.row.CreateTime | dateFormat}}
                  </template>
                </el-table-column>
                <el-table-column label="操作">
                  <template slot-scope="scope">
                    <el-button
                      size="mini"
                      icon="el-icon-edit"
                      @click="edit_item(scope.row)"
                    ></el-button>
                    <el-button
                      size="mini"
                      icon="el-icon-delete"
                      @click="del_item(scope.row)"
                    ></el-button>
                  </template>
                </el-table-column>
              </el-table>
            </template>
          </div>
          <div
            style="text-align: center;"
            class="block"
          >
            <el-pagination
              layout="prev, pager, next"
              :total="page.total"
              :page-size="page.limit"
              @current-change="page_change"
            >
            </el-pagination>
          </div>
          <el-dialog
            :title="title"
            :visible.sync="dialogVisible"
            width="45%"
            :before-close="handleClose"
            :append-to-body="true"
            :close-on-click-modal="false"
          >
            <el-form
              label-position="right"
              label-width="80px"
              :model="formLabelAlign"
            >
              <el-form-item
                label="医院id"
                style="display: inline-block;width:50%"
              >
                <el-input
                  style="display: inline-block;width:100%"
                  v-model="formLabelAlign.HospitalId"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="术语编码"
                style="display: inline-block;width:50%"
              >
                <el-input
                  style="display: inline-block;width:100%"
                  v-model="formLabelAlign.TermCode"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="术语含义"
                style="display: inline-block;width:50%"
              >
                <el-input
                  style="display: inline-block;width:100%"
                  v-model="formLabelAlign.TermName"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="版本号"
                style="display: inline-block;width:50%"
              >
                <el-input
                  style="display: inline-block;width:100%"
                  v-model="formLabelAlign.Version"
                ></el-input>
              </el-form-item>
              <el-form-item
                label="备注"
                style="display: inline-block;width:50%"
              >
                <el-input
                  style="display: inline-block;width:100%"
                  v-model="formLabelAlign.Remark"
                ></el-input>
              </el-form-item>
              <!-- <el-form-item
                label="状态"
                style="display: inline-block;width:50%"
              >
                <el-input
                  style="display: inline-block;width:100%"
                  v-model="formLabelAlign.Status"
                ></el-input>
              </el-form-item> -->
            </el-form>
            <span
              slot="footer"
              class="dialog-footer"
            >
              <el-button @click="handleClose">取 消</el-button>
              <el-button
                type="primary"
                @click="btn_yes"
              >确 定</el-button>
            </span>
          </el-dialog>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  data () {
    return {
      value: '',
      tableData: [],
      page: {
        page: 1,
        limit: 10,
        total: 1
      },
      dialogVisible: false,
      formLabelAlign: {},
      title: ''
    };
  },
  methods: {
    getTableData () {
      this.$post("/Term/PageList?keyword=" + this.value + "&page=" + this.page.page + "&limit=" + this.page.limit).then(res => {
        if (res.code == 0) {
          this.tableData = res.data.list
          this.page.total = res.data.TotalCount
        } else if (res.code == -1) {
          this.$message.error(res.msg);
        }
      })
    },
    page_change (val) {
      this.page.page = val
      this.getTableData()
    },
    open () {
      this.formLabelAlign.Id ? this.title = '修改' : this.title = '新增'
      this.dialogVisible = true
    },
    handleClose () {
      this.dialogVisible = false
      this.formLabelAlign = {}
    },
    btn_yes () {
      console.log(this.formLabelAlign);
      let body = this.formLabelAlign
      let url = body.Id ? '/Term/Update' : '/Term/Create'
      this.$post(url, body).then(res => {
        if (res.code == 0) {
          this.getTableData()
          this.handleClose()
          this.$message({
            message: '操作成功',
            type: 'success'
          });
        } else if (res.code == -1) {
          this.$message({
            message: res.msg,
            type: 'error'
          });
        }
      })
    },
    edit_item (val) {
      console.log(val);
      this.formLabelAlign = val
      this.open()
    },
    del_item (val) {
      let id = val.Id
      this.$confirm("确定删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(_ => {
          this.$post("/Term/Delete?id=" + id).then(res => {
            if (res.code == 0) {
              this.getTableData()
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            }
          })
        })
        .catch(_ => { });

    }
  },
  mounted () {
    // document.getElementById("2").focus();
  },
  created () {
    if (this.$route.query.Token) {
      localStorage.setItem('Token', this.$route.query.Token)
    }
    this.getTableData()
  },
  filters: {
    dateFormat (val) {
      if (val == null || val == '') {
        return ''
      } else {
        let d = new Date(val) // val 为表格内取到的后台时间
        let month = d.getMonth() + 1 < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1
        let day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate()
        let hours = d.getHours() < 10 ? '0' + d.getHours() : d.getHours()
        let min = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes()
        let sec = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds()
        let times = d.getFullYear() + '-' + month + '-' + day + ' ' + hours + ':' + min + ':' + sec
        return times
      }
    }
  }
};
</script>
<style lang="scss">
.TermManagement {
  .top-input {
    display: inline-block;
    width: 350px;
  }

  .box-top {
    margin-top: 15px;
    margin-left: 5px;
  }
}
</style>
